<template>
  <Panel>
    <template #title>
      管理员信息
    </template>
    <template>
      <el-row>
        <el-col>管理员ID:</el-col>
        <el-col>{{ users.id }}</el-col>
      </el-row>
      <el-row>
        <el-col>账号:</el-col>
        <el-col>{{ users.account }}</el-col>
      </el-row>
      <el-row>
        <el-col>用户组:</el-col>
        <el-col>{{ users.userGroup }}</el-col>
      </el-row>
      <el-row>
        <el-col>创建时间:</el-col>
        <el-col>{{ users.ctime | format }}</el-col>
      </el-row>
      <el-row>
        <el-col>管理员头像:</el-col>
        <el-col>
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/users/avatar_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-col>
      </el-row>
      <el-row>
        <el-col></el-col>
        <el-col>
          <el-button type="primary" @click="save">保存头像</el-button>
        </el-col>
      </el-row>
    </template>
  </Panel>
</template>

<script>
import local from '../../utils/local.js'
import { editImg } from '../../api/users.js'
export default {
  data() {
    return {
      users: {},
      imageUrl: '',
      imgUrl: ''
    }
  },
  created() {
    this.users = JSON.parse(local.get('users'))
    // console.log(this.users)
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.imgUrl = res.imgUrl
      // console.log(this.imgUrl)
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg'
      const arr = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!arr.includes(file.type)) {
        this.$message.error('上传头像图片不符合!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return arr.includes(file.type) && isLt2M
    },
    // 修改头像
    async save() {
      const { code } = await editImg({
        imgUrl: this.imgUrl
      })
      if (code === 0) {
        this.$confirm('是否确认修改', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            setTimeout(() => {
              location.reload()
            }, 2500)
          })
          .catch(() => {})
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-row {
  display: flex;
  padding: 15px 15px;
  border-bottom: 1px solid #eee;
  align-items: center;
  .el-col:nth-child(1) {
    width: 100px;
    margin-right: 20px;
  }
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
